package com.demo.thread;

import java.util.Date;
import java.util.concurrent.*;

/**
 * 线程池异常事件
 * ThreadPoolExecutor 通过捕获afterExecute 来捕获异常
 *
 */
public class ThreadPoolExceptionQuestion {
    public static void main(String[] args) {
       // ExecutorService executorService = Executors.newFixedThreadPool(2);
        Date date = new Date(1563037810000L);
        System.out.printf("dime:"+date);


        ThreadPoolExecutor executorService =     new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>()
        ){
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                System.out.printf("线程【%s】遇到了异常，详情信息%s:",Thread.currentThread().getName(),t.getMessage());
            }
        };

        executorService.execute(()->{
            throw new RuntimeException("数据达到阀值");
        });

        //等待一秒钟，确保提交的任务完成
        try {
            executorService.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}
